#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

CODE=${1-""}

source ./env.gitlab
echo "${GITLAB_PROJECT}"

#更新代码库
if [ "${CODE}" = "" ]; then
    echo "scan prjs's demo"
else
    #copy code and commit
    rm -rf ./prjs/ || true

    #./api-init.sh || true

    cp -rf ${GITLAB_PROJECT}/src ./prjs/${GITLAB_PROJECT}/
    cp -f ${GITLAB_PROJECT}/pom.xml ./prjs/${GITLAB_PROJECT}/
    ./api-push.sh
fi

#TCA检查
:<<EOF
# docker-compose up -d nginx 启动TCA
# http://localhost:900/login 初始登录账号是 CodeDog，密码是 admin
 在TCA平台进行”仓库登记“ http://gitlab/qa/tca/demo
 使用tca-client客户端镜像容器进行扫描，镜像构建于：CodeAnalysis-cust/Dockerfile-client
EOF
# config.ini
#codedog.ini的从 http://localhost:900/ 配置下载下来
# docker run -itd -v ./prjs/${GITLAB_PROJECT}:/opt/code -v ./CodeAnalysis-cust/client/config.ini:/opt/client/config.ini -v ./CodeAnalysis-cust/client/codedog.ini:/opt/client/codedog.ini -v ./pkg/data:/opt/client/data --name tca-client --network host tca-client:2022
docker rm tca-client -f ||true
# jq中-是特殊符号tca-net串如何输入 TODO，用了| jq语法：https://www.systutorials.com/docs/linux/man/1-jq/
IP_GIT=$(docker inspect gitlab |jq '.[0].NetworkSettings.Networks|.["tca-net"].IPAddress'|sed -s 's/"//g')
IP_NGINX=$(docker inspect tcanginx |jq '.[0].NetworkSettings.Networks|.["tca-net"].IPAddress'|sed -s 's/"//g')
echo "git ip: $IP_GIT nginx ip: $IP_NGINX"
#出现扫描时一致锁住的情况：INFO-util.logutil: query result from server ...
# 将config.ini中的 URL=http://localhost:900 换成 IP_NGINX
./_3check.bat ${GITLAB_PROJECT}

echo "tca done"